<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1300" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="90">
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="请检数量" prop="faskqty">
                        <el-input v-model="faskqty" placeholder="请输入请检数量" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="合格数量" prop="fcomqty">
                        <el-input v-model="form.fcomqty" placeholder="请输入合格数量" clearable @input="input(1,$event)"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="不合格数量" prop="fnoqty">
                        <el-input v-model="form.fnoqty" placeholder="请输入不合格数量" clearable @input="input(2,$event)"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="检验项" prop="detail">
                <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="350" hideAdd hideDelete>
                    <el-table-column label="检验项目名称" prop="name" align="center" width="120"></el-table-column>
                    <el-table-column label="编号" prop="code" align="center" width="120"></el-table-column>
                    <el-table-column label="检验标准" prop="standard" align="center" width="120"></el-table-column>
                    <el-table-column label="方法" prop="method" align="center" width="120"></el-table-column>
                    <el-table-column prop="is_rejects" label="是否填写不良品" align="center" width="120">
                        <template #default="scope">
                            <el-switch v-model="scope.row.is_rejects" :active-value="1" :inactive-value="0" disabled></el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="检验单位" prop="unit" align="center" width="120"></el-table-column>
                    <el-table-column label="标准值" prop="stvalue" align="center" width="120"></el-table-column>
                    <el-table-column label="检验上限" prop="upper" align="center" width="120"></el-table-column>
                    <el-table-column label="检验下限" prop="lower" align="center" width="120"></el-table-column>
                    <el-table-column label="检验说明" prop="check_content" align="center" width="120"></el-table-column>
                    <el-table-column label="检验结果" prop="check_number" align="center" width="120">
                        <template #default="scope">
                            <el-input v-model="scope.row.check_number" placeholder="检验结果" clearable class="input"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="合格判定" prop="is_qualified" align="center" width="200">
                        <template #default="scope">
                            <el-radio-group v-model="scope.row.is_qualified" class="ml-4">
                                <el-radio :label="1" size="large">合格</el-radio>
                                <el-radio :label="0" size="large">不合格</el-radio>
                            </el-radio-group>
                        </template>
                    </el-table-column>
                    <el-table-column label="不良品项" prop="rejects_all" align="center" width="100">
                        <template #default="scope">
                            <el-button type="primary" size="small" @click="table_rejects(scope.row,scope.$index)" :disabled="scope.row.is_qualified == 1">添加</el-button>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <add-reason v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></add-reason>
</template>

<script>
import addReason from './addReason.vue';
export default {
  components: { addReason },
	emits: ['success', 'closed'],
	data() {
		return {
			dialog: {
				save: false
			},
			mode: "add",
			titleMap: {
				add: '新增检验',
				edit: '编辑检验',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			form: {
				id: "",
                fcomqty: "",
                fnoqty: "",
                detail: [],
			},
            addTemplate: {},
            faskqty: "",
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
                    if(this.mode == 'edit'){
                        res = await this.$API.production.checknum.edit.post(this.form);
                    }else{
                        res = await this.$API.production.checkorder.add.post(this.form);
                    }
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            this.faskqty = data.faskqty
            var res = await this.$API.production.checkorder.detail.get({id: data.id});
            let array = []
            res.data.check_project.map(item=>{
                array.push({
                    check_project_id: item.id,
                    code: item.code,
                    name: item.name,
                    group: item.group,
                    standard: item.standard,
                    method: item.method,
                    unit: item.unit,
                    stvalue: item.stvalue,
                    upper: item.upper,
                    lower: item.lower,
                    note: item.note,
                    is_rejects: item.is_rejects,
                    check_content: item.check_content,
                    rejects_all: []
                })
            })
			this.form.detail = array
		},
        table_rejects(row,index){
            this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(index)
			})
        },
        handleSuccess(data,index){
            console.log(data);
            this.form.detail[index].rejects_all = data.rejects
        },
        input(e,event){
            if(event > this.faskqty){
                this.$message.error('输入数量不能大于请检数量');
                if(e == 1){
                    this.form.fcomqty = this.faskqty
                    this.form.fnoqty = 0
                }else{
                    this.form.fnoqty = this.faskqty
                    this.form.fcomqty = 0
                }
                return
            }
            if(e == 1){
                this.form.fnoqty = Number(this.faskqty) - Number(event)
            }
            if(e == 2){
                this.form.fcomqty = Number(this.faskqty) - Number(event)
            }
        },
	}
}
</script>

<style lang="scss">
.el-input-group__append{
    padding: 0 8px;
}
</style>
